package Servlet.MainPage;

import BaseClass.PetInfo;
import DataBase.DataBase;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//查找
public class SearchServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取搜索的关键词
        String keyword = req.getParameter("keyword");
        boolean haveSearched = DataBase.getSearched(keyword);

        List<PetInfo> petInfo = DataBase.getSearchResult(keyword);
        HttpSession HS = req.getSession();

        if(haveSearched) {
            resp.sendRedirect("MainPage/SearchResult.jsp");
            HS.setAttribute("SearchResult",petInfo);
        }else {
            resp.sendRedirect("MainPage/SearchFailed.jsp");
        }
    }
}
